package org.llc.flink.wordcount.api.sinkTest

import java.util.Properties

import org.llc.flink.wordcount.api.SensorReading
import org.apache.flink.api.common.serialization.{SerializationSchema, SimpleStringSchema}
import org.apache.flink.streaming.api.scala._
import org.apache.flink.streaming.connectors.kafka.{FlinkKafkaConsumer011, FlinkKafkaProducer011}


object KafkaDataPipelineTest {
  def main(args: Array[String]): Unit = {
    val env = StreamExecutionEnvironment.getExecutionEnvironment
    env.setParallelism(1)

    // 读取数据
    val properties = new Properties()
    properties.setProperty("bootstrap.servers", "localhost:9092")
    properties.setProperty("group.id", "consumer-group")

    val inputStream = env.addSource(new FlinkKafkaConsumer011[String]("sensor", new SimpleStringSchema(), properties))

    // 基本转换
    val dataStream: DataStream[SensorReading] = inputStream
      .map(line => {
        val arr = line.split(",")
        SensorReading(arr(0).trim, arr(1).trim.toLong, arr(2).trim.toDouble)
      })

    // 写入kafka
    //    dataStream.map( data => data.toString )
    //      .addSink( new FlinkKafkaProducer011[String]("localhost:9092", "sinktest", new SimpleStringSchema()) )

//    dataStream.map(data => (data.id, data.temperature))
//      .addSink(new FlinkKafkaProducer011[(String, Double)]("localhost:9092", "sinktest", new SerializationSchema[(String, Double)] {
//        override def serialize(element: (String, Double)): Array[Byte] = s"id:${element._1} temp:${element._2}".toArray.map(_.toByte)
//      }))

    env.execute("kafka pipeline job")
  }
}
